package com.umeox.watch.moto.launcher.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.watch.moto.launcher.R;

import java.util.Locale;

public class DailNumberAdapter extends RecyclerView.Adapter<DailNumberAdapter.Holder> {

    public String[] getDataList() {
        return mDataList;
    }

    private String[] mDataList;

    private Context mContext;

    private OnItemClickListener mOnItemClickListener;


    public DailNumberAdapter(Context context, String[] datas) {
        mContext = context;
        mDataList = datas;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_dail_pad, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Holder viewHolder = holder;
        viewHolder.bind(mDataList[position]);


    }

    @Override
    public int getItemCount() {
        return mDataList.length;
    }

//    public void setDataList(List<String> dataList) {
//        mDataList.clear();
//        mDataList.addAll(dataList);
//        notifyDataSetChanged();
//    }


    protected class Holder extends RecyclerView.ViewHolder {
        private TextView mNumberTv;
        private TextView mNumberArTv;
        private View mItemView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            mNumberTv = itemView.findViewById(R.id.tv_number);
            mNumberArTv = itemView.findViewById(R.id.tv_number_ar);
            mItemView = itemView;
        }


        public void bind(String values) {
            mNumberTv.setText(values);
            if(values.equals("*") || values.equals("#") || values.equals("+") || values.equals(",")){
                mNumberArTv.setVisibility(View.GONE);
            }else{
                mNumberArTv.setVisibility(View.VISIBLE);
                mNumberArTv.setText(String.format(new Locale("ar"),"%d",Integer.parseInt(values)));
            }

            mItemView.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(values);
                }
            });
        }
    }


    public interface OnItemClickListener {
        void onItemClick(String value);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
